package cn.openea.perpartner.meily.web.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.util.UserContext;
import cn.openea.perpartner.meily.web.entity.ActivityConfig;
import cn.openea.perpartner.meily.web.entity.dto.RemoveDto;
import cn.openea.perpartner.meily.web.service.IActivityConfigService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 活动配置控制层
 *
 * @author Weird077
 * @since 2023-03-30 14:31:37
 */
@RestController
@RequestMapping("/activity/config")
public class ActivityConfigController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private IActivityConfigService activityConfigService;

    @ApiOperation(value = "活动列表")
    @PostMapping("/list")
    public TableDataInfo selectConfigList(@RequestBody ActivityConfig config) {
        List<ActivityConfig> list = activityConfigService.selectConfigList(config);
        return getDataTable(list);
    }

    @ApiOperation(value = "id查询")
    @GetMapping("{id}")
    public AjaxResult selectConfigById(@PathVariable Serializable id) {
        return success(this.activityConfigService.getById(id));
    }

    @ApiOperation(value = "key查询")
    @GetMapping("/get/{configKey}")
    public AjaxResult selectConfigByKey(@PathVariable String configKey) {
        ActivityConfig config = activityConfigService.selectConfigByKey(configKey);
        return BeanUtil.isNotEmpty(config)?success(config):error("未查询到数据");
    }

    @ApiOperation(value = "新增数据")
    @PostMapping("/save")
    public AjaxResult saveConfig(@RequestBody ActivityConfig config) {
        if( config.getConfigId()== null){
            config.setCreateUser(UserContext.getUserId());
        }
        config.setUpdateUser(UserContext.getUserId());
        return toAjax(activityConfigService.saveConfig(config));
    }
    @ApiOperation(value = "复制配置")
    @GetMapping("/copy/{configId}")
    public AjaxResult copyConfig(@PathVariable String configId) {

        return toAjax(activityConfigService.copyConfig(configId));
    }
    @ApiOperation(value = "删除数据")
    @PostMapping("/delete")
    public AjaxResult deleteConfig(@RequestBody RemoveDto removeDto) {
        return toAjax(activityConfigService.deleteConfig(removeDto));
    }


}

